#!/usr/bin/env python
from math import pow, cos, sin, pi, fabs
from pygame import transform
from Engine.math_ext import *

class Hand:
    def __init__(self, image, index, xoff, yoff):
        self.index = index
        self.xoff = xoff
        self.yoff = yoff
        self.start_xoff = self.xoff
        self.start_yoff = self.yoff
        self.direction = 0
        self.angle = 0
        self.image = image
        self.image_bak = image

        #guns
        self.gun = None
        self.switching_gun = 0 #stops everyother movement, do the switching thing
        self.gun_path_pos = 0
        self.gun_path_index = 0
        self.gun_to = None
        
        #Path
        self.path = 0
        self.min_n_ppos = -16
        self.max_n_ppos = 16
        self.min_g_ppos = -8
        self.max_g_ppos = 8
        if self.index == 1:
            self.pathpos = self.min_n_ppos
        else:
            self.pathpos = self.max_n_ppos
            
        
    def walk(self, speed):
        if self.switching_gun:
            self.do_swich()
            return
        
        speed /= float(3)

        if self.gun == None:
            max_ppos = self.max_n_ppos
            min_ppos = self.min_n_ppos
        else:
            max_ppos = self.max_g_ppos
            min_ppos = self.min_g_ppos
        
        if self.path == 0:
            if speed > 0:   
                if self.pathpos < max_ppos:
                    self.pathpos += speed
                else:
                    self.path = 1
            else:
                if self.pathpos > min_ppos:
                    self.pathpos += speed
                else:
                    self.path = 1
                
        else:
            if speed > 0:
                if self.pathpos > min_ppos:
                    self.pathpos -= speed
                else:
                    self.path = 0
            else:
                if self.pathpos < max_ppos:
                    self.pathpos -= speed
                else:
                    self.path = 0
        
        if self.gun == None:
            self.xoff = self.start_xoff + self.pathpos
            self.yoff = self.start_yoff + float(self.calcYoff(self.pathpos))
        else:
            self.xoff = self.gun.start_xoff + self.pathpos
            self.yoff = self.gun.start_yoff + float(self.calcYoff(self.pathpos))
            self.gun.update(self.xoff, self.yoff)          
        
    def calcYoff(self, x):
        return -float(1)/float(32) * pow(x, 2)
    
    
    def fall(self):
        if self.switching_gun:
            self.do_swich()
            return
        
        if self.gun == None:
            xoff_to = self.start_xoff
        else:
            xoff_to = self.gun.start_xoff
        if self.gun == None:
            yoff_to = 0
        else:
            yoff_to = 24
        if distance(self.xoff, self.yoff, xoff_to, yoff_to) < 2:
            return
        dir = direction(self.xoff, self.yoff, xoff_to, yoff_to, True)
        
        self.xoff += cos(dir) * 2
        self.yoff -= sin(dir) * 2
        
        if self.gun != None:
            self.gun.update(self.xoff, self.yoff)
        
        if self.index == 1:
            if self.gun == None:
                self.pathpos = self.min_n_ppos
            else:
                self.pathpos = self.min_g_ppos
        else:
            if self.gun == None:
                self.pathpos = self.max_n_ppos
            else:
                self.pathpos = self.max_g_ppos
    
    def stopWalking(self):
        if self.switching_gun:
            self.do_swich()
            return

        if self.gun == None:
            xoff_to = self.start_xoff
            yoff_to = self.start_yoff
        else:
            xoff_to = self.gun.start_xoff
            yoff_to = self.gun.start_yoff
            
        if distance(self.xoff, self.yoff, xoff_to, yoff_to) < 4:
            return
        dir = direction(self.xoff, self.yoff, xoff_to, yoff_to, True)

        self.xoff += cos(dir) * 4
        self.yoff -= sin(dir) * 4
        if self.gun != None:
            self.gun.update(self.xoff, self.yoff)
        
        if self.index == 1:
            if self.gun == None:
                self.pathpos = self.min_n_ppos
            else:
                self.pathpos = self.min_g_ppos
        else:
            if self.gun == None:
                self.pathpos = self.max_n_ppos
            else:
                self.pathpos = self.max_g_ppos
        
        
    def switchGun(self, gun):
        self.switching_gun = 1
        self.gun_to = gun
        self.gun_path_pos = self.xoff
        
    def do_swich(self):
        pathspeed = 4
        if self.gun == None:
            start_xoff = self.start_xoff
        else:
            start_xoff = self.gun.start_xoff
        if self.gun_path_index == 0:
            if self.direction == 0:
                if self.gun_path_pos > -8:
                    self.gun_path_pos -= pathspeed
                else:
                    self.gun = self.gun_to
                    if self.gun != None:
                        self.gun.set_direction(self.direction)
                    self.gun_path_index = 1
                    self.gun_path_pos = -8
            else:
                if self.gun_path_pos < 40:
                    self.gun_path_pos += pathspeed
                else:
                    self.gun = self.gun_to
                    if self.gun != None:
                        self.gun.set_direction(self.direction)
                    self.gun_path_index = 1
                    self.gun_path_pos = 40
            #print "1: x = " + str(self.gun_path_pos) + ", y = " + str(self.calc_gun_yoff(self.gun_path_pos, self.gun))

        else:
            if self.direction == 0:
                if self.gun_path_pos < start_xoff:
                    self.gun_path_pos += pathspeed
                else:
                    self.switching_gun = 0
                    if self.gun != None:
                        self.gun.set_angle(0)
                    self.gun_path_index = 0                  
                    self.gun_path_pos = start_xoff
            else:
                if self.gun_path_pos > start_xoff:
                    self.gun_path_pos -= pathspeed
                else:
                    self.switching_gun = 0
                    if self.gun != None:
                        self.gun.set_angle(0)
                    self.gun_path_index = 0
                    self.gun_path_pos = start_xoff
            #print "2: x = " + str(self.gun_path_pos) + ", y = " + str(self.calc_gun_yoff(self.gun_path_pos, self.gun))
        xoff_to = self.gun_path_pos
        yoff_to = self.calc_gun_yoff(xoff_to, self.gun)
        
        self.xoff = xoff_to
        self.yoff = yoff_to
        if self.gun != None:
            self.gun.update(self.xoff, self.yoff)
    
    def calc_gun_yoff(self, x, gun):
        if self.gun == None:
            return -float(0.4) * fabs(x - 20) + self.start_yoff
        else:
            return -float(0.4) * fabs(x - 20) + self.gun.start_yoff
    
    def shoot(self, level, x, y, speed, direction):
        self.gun.shoot(level, x, y, speed, direction)
    
    def change_angle(self, angle):
        if self.angle != angle:
            self.angle = angle
            self.image = transform.rotate(self.image_bak, angle)
            
    def set_direction(self, direction):
        if self.direction != direction:
            self.image = transform.flip(self.image_bak, True, False)
            self.direction = direction
            if self.gun != None:
                prev_s_xoff = self.gun.start_xoff
                prev_s_yoff = self.gun.start_yoff
                self.gun.set_direction(direction)
                #self.xoff = self.gun.start_xoff - (self.xoff - prev_s_xoff)
                #self.yoff = self.gun.start_yoff - (self.yoff - prev_s_yoff)
    
    def draw(self, camera, x, y):
        if self.gun == None:
            start_xoff = 32
        else:
            start_xoff = 32
        
        if self.switching_gun:
            if self.gun != None:
                self.gun.set_angle(float(270) / start_xoff * (start_xoff - self.gun_path_pos))
        
        if self.gun == None:
            camera.blit(self.image, (x + self.xoff, y + self.yoff))
        else:
            self.gun.draw(camera, (x, y))